package se.sll.itintegration.catalogue.gui.service.view.capacity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import se.sll.service.catalogue.common.Translator;
import se.sll.service.catalogue.common.rest.request.CapacityCreateRequest;

public class CapacityCreateRequestListCapacityTranslator implements Translator<CapacityViewCapacity, List<CapacityCreateRequest>> {
    private final String hsaID;
    private final String productId;
    private final Date validTo;

    public CapacityCreateRequestListCapacityTranslator(String hsaID, String productId, Date validTo) {
        this.hsaID = hsaID;
        this.productId = productId;
        this.validTo = validTo;
    }

    @Override
    public List<CapacityCreateRequest> translate(CapacityViewCapacity from) {
        return translateTo(from, new ArrayList<CapacityCreateRequest>());
    }

    @Override
    public List<CapacityCreateRequest> translateTo(CapacityViewCapacity from, List<CapacityCreateRequest> to) {
        String codeSystem = from.getCodeSystem();
        addCreateRequest(to, codeSystem, from.getWaitTimeCode(), from.getWaitTime());
        addCreateRequest(to, codeSystem, from.getWaitTimePriorityCode(), from.getWaitTimePriority());
        return to;
    }

    private void addCreateRequest(List<CapacityCreateRequest> dst, String codeSystem, String code, Integer value) {
        if (value != null) {
            CapacityCreateRequest request = new CapacityCreateRequest();
            request.setHsaID(hsaID);
            request.setProduct(productId);
            request.setValidTo(validTo);
            request.setCodeSystem(codeSystem);
            request.setCode(code);
            request.setValue(Integer.toString(value));
            dst.add(request);
        }
    }
}
